// pages/couponcenter/couponcenter.js
const api = require("../../utils/interface.js")
const math = require("../../utils/math.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,//切换tab
    currentIndex:null,
    couponList:[],//模拟
    activityId:null,
    gainStatus:null,//
    storeId:"",
  },
  godetails(e){
    console.log(e, "e")
    let couponId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/couponcenter/centerdetails/centerdetails?memberCouponId=' + couponId
    })
  },
  //模拟
  // 获取优惠券列表
  getCouponList(storeId) {
    wx.showToast({ title: '加载中', icon: 'loading' });
    let user = wx.getStorageSync('userInfo').id
    let that=this
    api.getCouponCenterActivity({ memberId: user,storeId:storeId}).then(res => {
      console.log(res)
      if (res.data.result.couponList.length > 0) {
        res.data.result.couponList.map((item, index) => {
          item.preAmount = math.divide(item.preAmount, 100)
          item.minimumAmount = math.divide(item.minimumAmount, 100)
          item.state=true
        })
        that.setData({
          couponList: res.data.result.couponList,
          activityId: res.data.result.activityId,
          memberId:user
        })
        wx.hideToast();
      }else{
        wx.showToast({
          title: '暂无优惠券！',
          icon: 'none',
          duration: 1500
        })
      }

    })
  },
  //点击优惠券时触发
  handClick(e){
    console.log(e,"e")
    let activityPositionId=e.currentTarget.dataset.id
    api.getCouponByCenter({ memberId: this.data.memberId, couponActivityPositionId:activityPositionId,activityId:this.data.activityId }).then(res => {
    if(res.data.status==200){
      wx.showToast({
        title: '领取成功！',
        icon: 'none',
        duration: 1500
      })
      this.getCouponList(this.data.storeId)
    }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let user = wx.getStorageSync('userInfo')
    if (user&&user.id) {
      this.data.storeId = options.stationId
      this.getCouponList(options.stationId)
    }else{
      let id = options.stationId?options.stationId:""
      wx.redirectTo({
        url: '/pages/login/login?page=pages/couponcenter/couponcenter&&state=1&stationId='+id
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})